<template>
  <div>
    <el-table :data="classifyArr" style="width: 100%" border>
      <el-table-column label="#" type="index" width="50"></el-table-column>
      <el-table-column :label="classifytext">
        <template slot-scope="scope">
          <div
            v-show="!scope.row.isInput"
            @dblclick="xiouFun(scope.row)"
            slot="reference"
            class="name-wrapper"
          >
            <el-tag size="medium">{{ scope.row[active?'type':'title']}}</el-tag>
          </div>
          <el-input
            v-show="scope.row.isInput"
            @blur="handleEdit(scope.row)"
            v-model="scope.row[active?'type':'title']"
            v-focus
            placeholder="请输入内容"
          ></el-input>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="200">
        <template slot-scope="scope">
          <el-button
            size="mini"
            style="margin-left: 10px;"
            v-show="scope.row.isFirst"
            type="success"
            @click="addClass(scope.row)"
          >保存</el-button>
          <el-button
            size="mini"
            v-show="!scope.row.isFirst"
            type="warning"
            @click="handleEdit(scope.row)"
          >编辑</el-button>
          <el-button
            size="mini"
            v-show="!scope.row.isFirst"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { mapActions, mapState } from "vuex";
export default {
  data() {
    return {
      lisMessage: {},
      disabled: true,
      test: true,
      classifytext: "",
      active: null
    };
  },
  mounted() {
    if (this.$route.name == "Classify") {
      this.classifytext = "类别";
      this.active = 1;
    } else {
      this.classifytext = "分类";
      this.active = 0;
    }
    this.getClassList(this.active);
  },
  computed: {
    ...mapState("classify", ["classifyArr"])
  },
  methods: {
    ...mapActions("classify", [
      "getClassList",
      "saveClassify",
      "addClassify",
      "deleteFun"
    ]),
    async addClass(row) {
      await this.addClassify({active:this.active,form:row});
      this.$message({
        type: "success",
        message: "新增成功!"
      });
      row.name = "";
    },
    xiouFun(row) {
      row.isInput = true;
      this.lisMessage.msg = this.active?row.type:row.title;
      this.lisMessage.id = row.id;
      console.log(this.lisMessage)
    },
    async handleEdit(row) {
      // console.log(row)
      if (row.isFirst) return;
      row.isInput = false;
      if (row.name == this.lisMessage.msg) return;
      if (!this.test) {
        this.test = true;
        return;
      }
      // console.log(this.lisMessage)
      if (this.lisMessage.id) {
       
       this.saveClassify({active:this.active,form:row}).then(v => {
          this.$message({
            type: "success",
            message: "修改成功!"
          });
          this.lisMessage = {};
        });
      } else {
        this.$message({
          type: "info",
          message: "请双击类别进行修改！"
        });
      }
      this.test = false;
      // if (!row.name.trim()) {
      //   this.deleteFun(row.id);
      // }
    },
    handleDelete(index, row) {
      this.$confirm("此操作将永久删除该项信息, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
           this.deleteFun({active:this.active,id:row.id}).then(res => {
            this.$message({
              type: "success",
              message: "删除成功!"
            });
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    }
  },
  directives: {
    focus(el) {
      el.focus();
    }
  },
  async created() {}
};
</script>

<style scoped>
</style>